package com.iukcy.hight.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.iukcy.hight.dao.TClassMapper;
import com.iukcy.hight.model.TClass;
import com.iukcy.hight.service.ClassService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: kbase-Utils
 * @description: 课程服务实现
 * @author: zhangzhufu
 * @create: 2020-08-11 09:03:19
 **/
@Service
public class ClassServiceImpl extends ServiceImpl<TClassMapper, TClass> implements ClassService {

    @Autowired
    TClassMapper classDao;

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List<TClass> getAll() {
        EntityWrapper<TClass> wrapper = new EntityWrapper<>();
        return classDao.selectList(wrapper);
    }

    @Override
    public List<TClass> getSO(String name) {
        return classDao.getSOm(name);
    }

    @Override
    public void batchAdd(List<TClass> classes) {
        //批量 insert
        SqlSession session = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        TClassMapper batchLineMapper = session.getMapper(TClassMapper.class);
        try {
            classes.forEach(tClass -> batchLineMapper.insert(tClass));
            session.commit();
            session.clearCache();
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            session.close();
        }
    }
}